﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HdipCricketClub.Models
{
    public class FixtureRepository
    {
        CricketClubEntities db = new CricketClubEntities();

        public Fixture GetFixture(int id)
        {
            return db.Fixtures.Single(f => f.fixture_id == id);
        }

        public IQueryable<Fixture> FindAllFixtures()
        {
            return db.Fixtures.Include("Team").Include("Opponent").OrderBy(f => f.date).ThenBy(f => f.start_time);
        }

        public IQueryable<Fixture> FindFixtureScorecards()
        {
            return db.Fixtures.Include("Team").Include("Opponent").Include("ScorecardHeader").Where(f => f.ScorecardHeader.scorecard_entered == true).OrderBy(f => f.date);
        }

        public IQueryable<Fixture> FindFixturesWithNoTeamSelected()
        {
            // Find fixtures with teams selected

            var fixturesWithTeams = db.ClubFixturePlayers.Select(f => f.fixture_id).Distinct();

            // Find All Fixtures

            var allFixtures = db.Fixtures;

            List<Fixture> fixtures = new List<Fixture>();

            foreach (Fixture fixture in allFixtures)
            {
                if (!fixturesWithTeams.Contains(fixture.fixture_id))
                {
                    fixtures.Add(fixture);
                }
            }

            return fixtures.AsQueryable();
        }

        public List<Fixture> FindFixturesWithNoScoreCard()
        {
            // Find Fixtures that have a team selected

            List<int> fixturesWithTeamsIds = db.ClubFixturePlayers.Select(f => f.fixture_id).Distinct().ToList();

            // Find Fixtures that have no scorecard entered

            List<Fixture> fixturesWithNoScoreCard = db.Fixtures.Include("Team").Include("Opponent").Include("ScorecardHeader")
                                                     .Where(f => f.ScorecardHeader.scorecard_entered == null || f.ScorecardHeader.scorecard_entered == false).OrderBy(f => f.date).ToList();

            // Iterate through teams with no scorecard to see if a team has been selected

            List<Fixture> fixtures = new List<Fixture>();

            foreach (Fixture fixture in fixturesWithNoScoreCard)
            {
                if (fixturesWithTeamsIds.Contains(fixture.fixture_id))
                {
                    fixtures.Add(fixture);
                }
            }
            return fixtures;
        }

        public List<Fixture> FindUpcomingFixturesWithTeams()
        {
            List<Fixture> allFixtures = db.Fixtures.ToList();

            List<int> fixturesWithTeams = db.ClubFixturePlayers.Select(cfp => cfp.fixture_id).Distinct().ToList();

            List<Fixture> fixtures = new List<Fixture>();

            foreach (Fixture fixture in allFixtures)
            {
                if (fixturesWithTeams.Contains(fixture.fixture_id))
                {
                    fixtures.Add(fixture);
                }
            }

            return fixtures;
        }

        public Fixture ParseFormCollection(FormCollection formCollection)
        {
            string[] fixtureId = formCollection.GetValues("ScorecardFixture.fixture_id");

            Fixture fixture = GetFixture(Int32.Parse(fixtureId[0]));

            return fixture;
        }

        public Fixture ParseScorecardFormCollection(FormCollection formCollection)
        {
            string[] formCollectionFixtureId = formCollection.GetValues("ScorecardFixture.fixture_id");

            Fixture fixture = GetFixture(Int32.Parse(formCollectionFixtureId[0]));

            return fixture;
        }
    }
}